package com.wn.netty.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 * @FileName: NIOClientTest
 * @Description: TODO
 * @Author: MaWenHao
 * @Created: 2020/7/24 - 16:53
 * @Version: V1.0
 * @Company: 东方微银科技（北京）有限公司
 */
public class NIOClientTest {

    public static void main(String[] args) {
        InetSocketAddress address = new InetSocketAddress("localhost", 8888);
        try (SocketChannel socketChannel = SocketChannel.open();) {
            socketChannel.configureBlocking(false);
            if (!socketChannel.connect(address)) {
                while (!socketChannel.finishConnect()) {
                    System.out.println("客户端连接未完成,不会阻塞线程,可执行其他代码");
                }
            }

            String str = "hello world";
            ByteBuffer buffer = ByteBuffer.wrap(str.getBytes());
            socketChannel.write(buffer);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
